<?php
/**
 * 继承修改input类
 *
 * @author calvin<hongbinfu@163.com>
 */
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Input Class
 *
 * Pre-processes global input data for security
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Input
 * @author		EllisLab Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/input.html
 */
class MY_Input extends CI_Input
{


    /**
     * Request Headers
     *
     * @param	bool	$xss_clean	Whether to apply XSS filtering
     * @return	array
     *
     * @modify header的所有可以都是驼峰
     */
    public function request_headers($xss_clean = FALSE)
    {
        // If header is already defined, return it immediately
        if ( ! empty($this->headers))
        {
            return $this->headers;
        }
        if (function_exists('apache_request_headers'))
        {
            $apache_headers = apache_request_headers();
            $headers=array();
            foreach($apache_headers as $k=>$v)
            {
                $ks = explode("-",$k);
                $rk = array();
                foreach($ks as $kk)
                {
                    $rk[]=ucwords($kk);
                }
                $rk = implode("-",$rk);
                $headers[$rk] = $v;
            }
            return $this->headers = $headers;
        }

        $this->headers['Content-Type'] = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : @getenv('CONTENT_TYPE');

        foreach ($_SERVER as $key => $val)
        {
            if (sscanf($key, 'HTTP_%s', $header) === 1)
            {
                // take SOME_HEADER and turn it into Some-Header
                $header = str_replace('_', ' ', strtolower($header));
                $header = str_replace(' ', '-', ucwords($header));
                $this->headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
            }
        }

        return $this->headers;
    }


    /**
     * Fetch from array
     *
     * Internal method used to retrieve values from global arrays.
     *
     * @param	array	&$array		$_GET, $_POST, $_COOKIE, $_SERVER, etc.
     * @param	mixed	$index		Index for item to be fetched from $array
     * @param	bool	$xss_clean	Whether to apply XSS filtering
     * @return	mixed
     */
    protected function _fetch_from_array(&$array, $index = NULL, $xss_clean = NULL)
    {
        is_bool($xss_clean) OR $xss_clean = $this->_enable_xss;

        // If $index is NULL, it means that the whole $array is requested
        isset($index) OR $index = array_keys($array);

        // allow fetching multiple keys at once
        if (is_array($index))
        {
            $output = array();
            foreach ($index as $key)
            {
                $output[$key] = $this->_fetch_from_array($array, $key, $xss_clean);
            }

            return $output;
        }

        if (isset($array[$index]))
        {
            $value = $array[$index];
        }
        elseif (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
        {
            $value = $array;
            for ($i = 0; $i < $count; $i++)
            {
                $key = trim($matches[0][$i], '[]');
                if ($key === '') // Empty notation will return the value as array
                {
                    break;
                }

                if (isset($value[$key]))
                {
                    $value = $value[$key];
                }
                else
                {
                    return "";
                }
            }
        }
        else
        {
            return "";
        }

        return ($xss_clean === TRUE)
            ? $this->safe_produce($value)
            : $value;
    }


    public function safe_produce($value)
    {
        $value = $this->security->xss_clean($value);
        $value = is_string($value) && !empty($value)?htmlspecialchars($value,ENT_QUOTES):$value;
        return $value;
    }

}